package com.ayf.payment.game.api.service.platform.impl;

import com.ayf.payment.game.api.dto.platform.AdminUserDTO;
import com.ayf.payment.game.api.dto.platform.criteria.AdminUserCriteria;
import com.ayf.payment.game.api.dto.platform.criteria.AdminUserPageCriteria;
import com.ayf.payment.game.api.entity.platform.AdminLog;
import com.ayf.payment.game.api.entity.platform.AdminUser;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.platform.AdminUserMapper;
import com.ayf.payment.game.api.service.platform.AdminLogService;
import com.ayf.payment.game.api.service.platform.AdminUserService;
import com.ayf.payment.game.api.utils.MD5Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swwx.charm.commons.lang.utils.BeanConvertUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Resource
    private AdminUserMapper adminUserMapper;

    @Resource
    private AdminLogService adminLogService;

    @Override
    public AdminUser login(AdminUserCriteria criteria) {
        if (criteria == null) {
            throw new ParameterException("登录账号不能为空");
        }
        if (criteria.getLoginAccount() == null) {
            throw new ParameterException("登录账号不能为空");
        }
        if (criteria.getPassword() == null) {
            throw new ParameterException("登录密码不能为空");
        }
        AdminUser adminUser = new AdminUser();
        adminUser.setLoginAccount(criteria.getLoginAccount());
        adminUser = adminUserMapper.selectOne(adminUser);
        if (adminUser == null) {
            throw new ParameterException("账号或密码错误");
        }
        String sign = MD5Util.sign(criteria.getPassword(), adminUser.getSalt());
        if (adminUser.getPassword().equals(sign)) {
            AdminUser adminUserDb = new AdminUser();
            adminUserDb.setId(adminUser.getId());
            adminUserDb.setLoginTime(new Date());
            adminUserDb.setLoginIp(criteria.getLoginIp());
            adminUserMapper.updateByPrimaryKeySelective(adminUserDb);
            AdminLog adminLog=new AdminLog(adminUser.getLoginAccount(),adminUser.getId(),0,criteria.getLoginIp(),"登录成功","登录",new Date());
            adminLogService.insert(adminLog);
            return adminUser;
        } else {
            AdminLog adminLog=new AdminLog(criteria.getLoginAccount(),0,0,criteria.getLoginIp(),"登录失败：账号或密码错误","登录",new Date());
            adminLogService.insert(adminLog);
            throw new ParameterException("账号或密码错误");
        }

    }

    @Override
    public void insert(AdminUserCriteria criteria, AdminUserDTO adminUserDTO) {
        Example example = new Example(AdminUser.class);
        example.createCriteria().andEqualTo("loginAccount",criteria.getLoginAccount());
        AdminUser db=adminUserMapper.selectOneByExample(example);
        if(db!=null){
        throw new ParameterException("账户已存在！");

        }
        AdminUser adminUser = new AdminUser();
        adminUser.setLoginAccount(criteria.getLoginAccount());
        adminUser.setRealName(criteria.getRealName());
        adminUser.setPhone(criteria.getPhone());
        adminUser.setEmail(criteria.getEmail());
        adminUser.setQq(criteria.getQq());
        adminUser.setRoleId(criteria.getRoleId());
        adminUser.setAccountNumber(criteria.getAccountNumber());
        adminUser.setUserStatus((short) 1);
        adminUser.setCreateTime(new Date());
        adminUser.setRemark(criteria.getRemark());
        String salt = UUID.randomUUID().toString().replaceAll("-", "").substring(0,8);
        adminUser.setSalt(salt);
        String pwd = MD5Util.sign(criteria.getPassword(), adminUser.getSalt());
        adminUser.setPassword(pwd);
        adminUserMapper.insert(adminUser);
        AdminLog adminLog=new AdminLog(adminUserDTO.getLoginAccount(),adminUserDTO.getId(),0,criteria.getLoginIp(),"创建用户："+adminUser.getLoginAccount(),"创建用户",new Date());
        adminLogService.insert(adminLog);
    }

    @Override
    public void del(AdminUser adminUser) {
        adminUserMapper.deleteByPrimaryKey(adminUser.getId());
    }

    @Override
    public void update(AdminUserCriteria criteria, AdminUserDTO adminUserDTO) {

        AdminUser adminUser=new AdminUser();
        adminUser.setId(criteria.getId());
        adminUser.setAccountNumber(criteria.getAccountNumber());
        adminUser.setPhone(criteria.getPhone());
        adminUser.setEmail(criteria.getEmail());
        adminUser.setRealName(criteria.getRealName());
        adminUser.setQq(criteria.getQq());
        adminUser.setRoleId(criteria.getRoleId());
        adminUser.setRemark(criteria.getRemark());
        adminUser.setLastUpdateTime(new Date());
        adminUserMapper.updateByPrimaryKeySelective(adminUser);
        AdminLog adminLog=new AdminLog(adminUserDTO.getLoginAccount(),adminUserDTO.getId(),0,criteria.getLoginIp(),"更新用户："+adminUser.getAccountNumber(),"更新用户",new Date());
        adminLogService.insert(adminLog);
    }

    @Override
    public AdminUser selectOne(AdminUser adminUser) {
        return adminUserMapper.selectOne(adminUser);
    }

    @Override
    public AdminUserDTO selectOneDTO(AdminUser adminUser) {
        if (adminUser == null) {
            throw new ParameterException("用户信息不能为空");
        }
        AdminUser user= adminUserMapper.selectOne(adminUser);
        return BeanConvertUtils.convert(user, AdminUserDTO.class);
    }

    @Override
    public PageInfo<AdminUserDTO> selectAllPage(AdminUserPageCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<AdminUserDTO> list = adminUserMapper.selectAllPage(criteria);
        return new PageInfo<AdminUserDTO>(list);

    }

    @Override
    public void updatePWD(AdminUserCriteria criteria) {
        AdminUser adminUser = adminUserMapper.selectByPrimaryKey(criteria.getId());
        String sign = MD5Util.sign(criteria.getPassword(), adminUser.getSalt());
        adminUser.setPassword(sign);
        adminUserMapper.updateByPrimaryKeySelective(adminUser);
    }
}
